/**
 * Created by json on 2016/9/28.
 */
xzdd.mj_uiGameScene = cc.Scene.extend({
    sprite:null,
    seatNumList:[],
    ctor:function () {
        this._super();
        this.mainNode=ccs.load(dwc.g_arrGCRes.mj4_gamescene_json).node;
        this.addChild(this.mainNode);
        this.initMain(this.mainNode);
    },
    initMain: function (mainNode) {
        var that=this;
        //test
        this.schedule(that.OnTest,3,40,2);
        this.num=-1;
        this.index=0;
        //test end
        this.initTable();

    },
    OnTest: function () {
        this.num+=1;
        if(this.num>=4){
            this.num=0;
            this.index++;
        }
        this.OnSubChuPai(this.num,this.index);
    },
    initTable: function () {
        //test 逆时针从自己开始
        var cardNumList=[14,13,13,13];
        var selfList=["#p4b1_1.png","#p4b1_2.png","#p4b1_3.png","#p4b1_4.png","#p4b1_5.png","#p4b2_1.png","#p4b2_2.png","#p4b2_3.png","#p4b2_4.png","#p4b2_5.png","#p4b3_1.png","#p4b3_2.png","#p4b3_3.png","#p4b3_4.png"];
        var rightList=[];
        var topList=[];
        var leftList=[];
        //四个人新牌坐标
        this.newCardPos=[];
        var cardinfo={};
        cardinfo.type=2;
        cardinfo.index=2;
        //test end

        //四个人的牌
        for(var i=0;i<4;i++){
            for(var j=0;j<cardNumList[i];j++){
                switch (i){
                    case 0:
                        var card=new mj4.HAND_CARD(cardinfo,true,this.OnSureCardCallBack,this);
                        card.setPosition(100+j*80,70);
                        if(j==13){
                            card.setPosition(100+13*80+50,70);
                        }
                        this.addChild(card,10);

                        break;
                    case 1:
                        var card=new cc.Sprite("#tbgs_1.png");
                        card.setPosition(80+13*80,200+j*30);
                        if(j==13){
                            card.setPosition(80+13*80,200+j*30+20);
                        }
                        this.addChild(card,10);
                        break;
                    case 2:
                        var card=new cc.Sprite("#tbgs_2.png");
                        card.setPosition(1280-300-j*35,720-50);
                        if(j==13){
                            card.setPosition(1280-300-j*35-20,720-50);
                        }
                        this.addChild(card,10);
                        break;
                    case 3:
                        var card=new cc.Sprite("#tbgs_3.png");
                        card.setPosition(300,720-120-j*30);
                        if(j==13){
                            card.setPosition(300,720-120-j*30-20);
                        }
                        this.addChild(card,10);
                        break;
                    default:
                        break;
                }
            }

            this.initOutCard(i);
        }
        this.newCardPos=[cc.p(100+13*80+50,70),
            cc.p(80+13*80,200+13*30+20),
            cc.p(1280-300-13*35-20,720-50),
            cc.p(300,720-120-13*30-20)];
        this.initFourNewCard();
        this.initFourOutCard();
    },

    initFourNewCard: function () {
        this.newCardList=[];
        var pngStr="";
        for(var i=0;i<4;i++){
            switch (i){
                case 0:
                    pngStr="#p4b1_1.png";
                    break;
                case 1:
                    pngStr="#tbgs_1.png";
                    break;
                case 2:
                    pngStr="#tbgs_2.png";
                    break;
                case 3:
                    pngStr="#tbgs_3.png";
                    break;
                default:
                    break;
            }
            var card=new cc.Sprite(pngStr);
            card.setPosition(this.newCardPos[i]);
            this.addChild(card,10);
            card.setVisible(false);
            this.newCardList[i]=card
        }
    },
    initFourOutCard: function () {
        this.fourCardList=[];
        var posList=[cc.p(1280/2,160),cc.p(1280-300,720/2-70),cc.p(1280/2,720-200),cc.p(400,720/2-50)];
        for(var i=0;i<4;i++){
            var card=new cc.Sprite("#p4s1_1.png");
            card.setPosition(posList[i]);
            card.setVisible(false);
            this.addChild(card,11);
            this.fourCardList[i]=card;
        };
    },
    //创建好打出的牌
    initOutCard: function (idx) {
        var that=this;
        switch (idx){
            case 0:
                this.selfOutList=[];
                //test
                var cardinfo={};
                cardinfo.num=4;
                cardinfo.type=1;
                cardinfo.index=1;
                //test end
                for(var i=0;i<2;i++){
                   for(var j=0;j<7;j++){
                       var card=new mj4.OUT_CARD(cardinfo);
                       card.setPosition(500+j*50,260-i*60);
                       that.addChild(card,9);
                       card.setVisible(false);
                       that.selfOutList[i*7+j]=card;
                   }
                }
                this.seatNumList.push(that.selfOutList);
                break;
            case 1:
                this.rightOutList=[];
                //test
                var cardinfo={};
                cardinfo.num=1;
                cardinfo.type=1;
                cardinfo.index=1;
                //test end
                for(var i=0;i<2;i++){
                    for(var j=0;j<7;j++){
                        var card=new mj4.OUT_CARD(cardinfo);
                        card.setPosition(13*80-49+i*45,250+7*35-j*25);
                        that.addChild(card,9);
                        card.setVisible(false);
                        that.rightOutList[i*7+j]=card;
                    }
                }
                this.seatNumList.push(that.rightOutList);
                break;
            case 2:
                this.topOutList=[];
                //test
                var cardinfo={};
                cardinfo.num=2;
                cardinfo.type=1;
                cardinfo.index=1;
                //test end
                for(var i=0;i<2;i++){
                    for(var j=0;j<7;j++){
                        var card=new mj4.OUT_CARD(cardinfo);
                        card.setPosition(1280-400-j*35,720-130-i*40);
                        that.addChild(card,9);
                        card.setVisible(false);
                        that.topOutList[i*7+j]=card;
                    }
                }
                this.seatNumList.push(that.topOutList);
                break;
            case 3:
                this.leftOutList=[];
                //test
                var cardinfo={};
                cardinfo.num=3;
                cardinfo.type=1;
                cardinfo.index=1;
                //test end
                for(var i=0;i<2;i++){
                    for(var j=0;j<7;j++){
                        var card=new mj4.OUT_CARD(cardinfo);
                        card.setPosition(350+i*45,720-200-j*25);
                        that.addChild(card,9);
                        card.setVisible(false);
                        that.leftOutList[i*7+j]=card;
                    }
                }
                this.seatNumList.push(that.leftOutList);
                break;
            default :
                break;
        }
    },

    OnSureCardCallBack: function (target) {
        cc.log("出牌了!!!!!!!!!!!!!!!!!!!!!!");


    },
    OnSubChuPai: function (num,idx) {
        var that=this;
        for(var i=0;i<that.seatNumList.length;i++){
            for(var j=0;j<that.seatNumList[i].length;j++){
                that.seatNumList[i][j].showNotCurrent();
            }
        }
        that.newCardList[num].setVisible(false);
        that.fourCardList[num].runAction(cc.sequence(cc.show(),cc.delayTime(1),cc.callFunc(function () {
            that.fourCardList[num].setVisible(false)
            that.seatNumList[num][idx].setVisible(true);
            that.seatNumList[num][idx].showCurrent();
            //test 测试显示下一个人发的牌
            if(num==3){
                num=-1;
            }
            that.newCardList[num+1].setVisible(true);
            //test  end
        },this)))


    },

    onEnter: function () {
        this._super();
    },

    onExit: function () {
        this._super();

    }
});
